package com.csist.servlet;

import com.csist.entity.Class;
import com.csist.entity.Teacher;
import com.csist.entity.Users;
import com.csist.service.UserService;
import com.csist.service.impl.UserServiceImpl;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "LoginServlet", urlPatterns= "/LoginServlet")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
login(request,response);
    }
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    }
    private void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        String userpass = request.getParameter("userpass");
        String usertype = request.getParameter("type");
        int num=0;
        if(usertype.equals("超级管理员")){
            num=1;
        }else if (usertype.equals("辅导管理员")){
            num=2;
        }else if(usertype.equals("宿舍管理员")){
            num=3;
        }
        if(username != null && username.trim().length() > 0 && username != null && username.trim().length() >=6) {
            Users user = new Users();
            Teacher teacher=new Teacher();
            UserService userService = new UserServiceImpl();
            if (userService.login(username,userpass,usertype)!=null){
                request.setAttribute("username",username);
                if (num==1){
                    response.sendRedirect(request.getContextPath()+"/main.html");
                }else if(num==2){
                    request.setAttribute("CountClass",userService.getCountClass(teacher.getTeaClass()));
                    response.sendRedirect(request.getContextPath()+"/main.html");
                }else if(num==3){
                    response.sendRedirect(request.getContextPath()+"/main.html");
                }
            }else {

            }

        }

    }
}
